<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>SpringAOP | 梓琰苑</title><meta name="keywords" content="Spring"><meta name="author" content="Milk Yang"><meta name="copyright" content="Milk Yang"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="[TOC] Spring的AOPAOP实现可分为两类   静态AOP实现AOP框架在编译阶段对程序进行修改，即实现对目标类型的增强，生成静态的AOP代理类（生成的*.class文件已经被改掉了，需要使用特定的编译器）。以AspectJ代表。 动态AOP实现AOP框架在巡行阶段动态生成AOP代理（在内存中以JDK动态代理或cglib动态地生成AOP代理类），以实现对目标对象的增强。以SpringAO">
<meta property="og:type" content="article">
<meta property="og:title" content="SpringAOP">
<meta property="og:url" content="http://yzb317415.gitee.io/blog/2022/06/22/SpringAOP/index.html">
<meta property="og:site_name" content="梓琰苑">
<meta property="og:description" content="[TOC] Spring的AOPAOP实现可分为两类   静态AOP实现AOP框架在编译阶段对程序进行修改，即实现对目标类型的增强，生成静态的AOP代理类（生成的*.class文件已经被改掉了，需要使用特定的编译器）。以AspectJ代表。 动态AOP实现AOP框架在巡行阶段动态生成AOP代理（在内存中以JDK动态代理或cglib动态地生成AOP代理类），以实现对目标对象的增强。以SpringAO">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="">
<meta property="article:published_time" content="2022-06-22T10:06:45.841Z">
<meta property="article:modified_time" content="2018-11-05T09:22:56.000Z">
<meta property="article:author" content="Milk Yang">
<meta property="article:tag" content="Spring">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content=""><link rel="shortcut icon" href="/blog/img/favicon.png"><link rel="canonical" href="http://yzb317415.gitee.io/blog/2022/06/22/SpringAOP/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/blog/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/blog/',
  algolia: undefined,
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery@2/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery@2/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'SpringAOP',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2018-11-05 17:22:56'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><meta name="generator" content="Hexo 6.2.0"></head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="https://i.loli.net/2021/02/24/5O1day2nriDzjSu.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/blog/archives/"><div class="headline">文章</div><div class="length-num">8</div></a><a href="/blog/tags/"><div class="headline">标签</div><div class="length-num">2</div></a><a href="/blog/categories/"><div class="headline">分类</div><div class="length-num">1</div></a></div><hr/></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/blog/">梓琰苑</a></span><div id="menus"><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">SpringAOP</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2022-06-22T10:06:45.841Z" title="发表于 2022-06-22 18:06:45">2022-06-22</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2018-11-05T09:22:56.000Z" title="更新于 2018-11-05 17:22:56">2018-11-05</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/blog/categories/%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/">学习笔记</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="SpringAOP"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><p>[TOC]</p>
<h1 id="Spring的AOP"><a href="#Spring的AOP" class="headerlink" title="Spring的AOP"></a>Spring的AOP</h1><p>AOP实现可分为两类</p>
<blockquote>
<ul>
<li>静态AOP实现<br>AOP框架在编译阶段对程序进行修改，即实现对目标类型的增强，生成静态的AOP代理类（生成的*.class文件已经被改掉了，需要使用特定的编译器）。以AspectJ代表。</li>
<li>动态AOP实现<br>AOP框架在巡行阶段动态生成AOP代理（在内存中以JDK动态代理或cglib动态地生成AOP代理类），以实现对目标对象的增强。以SpringAOP为代表</li>
</ul>
</blockquote>
<p>一般来说，静态AOP实现具有较好的性能，但需要使用特殊的编译器。动态AOP实现是纯Java实现，因此无须特殊的编译器，但是通常性能较差。</p>
<h2 id="AOP的基本概念"><a href="#AOP的基本概念" class="headerlink" title="AOP的基本概念"></a>AOP的基本概念</h2><p>术语:</p>
<blockquote>
<ul>
<li>切面（Aspect）:切面用于组织多个Advice，Advice放在切面中定义</li>
<li>连接点（Joinpoint）：程序执行过程中明确的店，如方法的调用，或者异常的抛出。在SpringAOP中，连接点总是方法的调用</li>
<li>增强处理（Advice）：AOP框架在特定的切入点执行的增强处理。处理有“around”、“before”、“after”等类型</li>
<li>切入点（Ponitcut）：可以插入增强处理的连接点。简而言之，当某个连接点满足指定要求时，该连接点将被添加增强处理。该连接点也就变成了切入点</li>
<li>引入：将方法或字段添加到被处理的类中</li>
<li>目标对象：被AOP框架进行增强处理的对象。如果AOP框架采用的是动态代理AOP实现，那么该对象就是一个被代理的对象</li>
<li>AOP代理：AOP框架创建的对象，简单地说，代理就是对目标对象的加强</li>
<li>织入：将增强处添加到目标对象中，并创建一个被增强的对象的过程就是织入</li>
</ul>
</blockquote>
<p>Spring中默认使用Java动态代理来创建AOP代理，也可使用cglib代理（需要代理类而不是代理接口的时候）。<br>SpringAOP中只支持使用方法调用作为连接点。</p>
<p>AOP编程的步骤：</p>
<blockquote>
<ul>
<li>定义普通业务组件</li>
<li>定义切入点，一个切入点可能横切多个业务组件</li>
<li>定义增强处理，增强处理就是在 AOP 框架为普通业务组件织入的处理动作</li>
</ul>
</blockquote>
<h2 id="基于注解的“零配置”方式"><a href="#基于注解的“零配置”方式" class="headerlink" title="基于注解的“零配置”方式"></a>基于注解的“零配置”方式</h2><p>配置启动AspectJ自动代理的三种方式</p>
<blockquote>
<ol>
<li>XML方式:在<code>&lt;beans.../&gt;</code>中增加<code>&lt;aop:aspectj-autoprox/&gt;</code>子元素</li>
<li>使用注解方式：使用@EnableAspectAutoProxy修饰使用@Configuration修饰的Java配置类</li>
<li>通过Bean后处理器：配置AnnotationAwareAspectJAutoProxyCreator的Bean后处理器，该Bean后处理器将会为容器中符合条件的Bean生成AOP代理</li>
</ol>
</blockquote>
<h3 id="定义切面Bean"><a href="#定义切面Bean" class="headerlink" title="定义切面Bean"></a>定义切面Bean</h3><p>当启用了@AspectJ支持后，只要在Spring容器中配置一个带@Aspect注解的Bean，Spring将会自动识别该Bean，并将该Bean作为切面处理<br>在Spring容器中配置切面Bean，与配置普通Bean没有任何区别</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Aspect</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">LogAspect</span></span><br><span class="line">&#123;</span><br><span class="line">   <span class="comment">// 定义该类的其他内容 </span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>当使用@Aspect修饰一个Java类之后，Spring将不会把该Bean当成组件Bean处理，因此负责自动增强的Bean将会略过该Bean，不会对该Bean进行任何增强处理</p>
<h3 id="定义Before增强处理"><a href="#定义Before增强处理" class="headerlink" title="定义Before增强处理"></a>定义Before增强处理</h3><p>在一个切面类里使用@Before修饰一个方法时，该方法将作为Befo增强处理。使用@Befo修饰时，通常需要指定一个value属性，该属性指定一个切入点表达式（既可以是一个已有的切入点，也可以直接定义切入点表达式），用于指定该增强处理将被织入哪些切入点</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Aspect</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">LogAspect</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="meta">@Before(&quot;execution(* org.learn.service.impl.*.*(..)&quot;)</span></span><br><span class="line"><span class="meta">    public void authority()</span></span><br><span class="line"><span class="meta">    &#123;</span></span><br><span class="line"><span class="meta">        System.out.println(&quot;模拟执行权限检查&quot;);</span></span><br><span class="line"><span class="meta">    &#125;</span></span><br><span class="line"><span class="meta">&#125;</span></span><br></pre></td></tr></table></figure>
<p>使用Befor增强处理只能在目标方法执行之前织入增强，如果Before增强处理没有特殊处理，目标方法总会自动执行，如果Before处理需要组织目标方法的执行，可通过抛出一个异常实现。Before增强处理处理执行时，目标方法还没获得执行的机会，所以Before增强处理无法访问目标方法的返回值</p>
<h3 id="定义AfterReturning增强处理"><a href="#定义AfterReturning增强处理" class="headerlink" title="定义AfterReturning增强处理"></a>定义AfterReturning增强处理</h3><p>使用@AfterReturning可修饰AfterReturning增强处理，AfterReturning增强处理将在目标正常完成后被织入<br>使用@AfterReturning注解可指定如下两个常用属性：</p>
<blockquote>
<ul>
<li>pointcut&#x2F;value：这两个属性的作用是一样的，它们都是用于指定该切入点对应的切入表达式。当指定了pointcut属性之后，value属性值将会被覆盖</li>
<li>returning：该属性值指定一个形参名，用于表示Advice方法中可定义与此同名的形参，该形参可用于访问目标方法的返回值。除此之外，在Advice方法中定义该形参（代表目标方法的返回值）是指定的类型，会限制目标方法必须返回指定类型的值</li>
</ul>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Aspect</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">LogAspect</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="comment">// 匹配org.crazyit.app.service.impl包下所有类的、</span></span><br><span class="line">	<span class="comment">// 所有方法的执行作为切入点</span></span><br><span class="line">	<span class="meta">@AfterReturning(returning=&quot;rvt&quot;</span></span><br><span class="line"><span class="meta">		, pointcut=&quot;execution(* org.crazyit.app.service.impl.*.*(..))&quot;)</span></span><br><span class="line">	<span class="comment">// 声明rvt时指定的类型会限制目标方法必须返回指定类型的值或没有返回值</span></span><br><span class="line">	<span class="comment">// 此处将rvt的类型声明为Object，意味着对目标方法的返回值不加限制</span></span><br><span class="line">	<span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">log</span><span class="params">(Object rvt)</span></span><br><span class="line">	&#123;</span><br><span class="line">		System.out.println(<span class="string">&quot;获取目标方法返回值:&quot;</span> + rvt);</span><br><span class="line">		System.out.println(<span class="string">&quot;模拟记录日志功能...&quot;</span>);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="定义AfterThrowing增强处理"><a href="#定义AfterThrowing增强处理" class="headerlink" title="定义AfterThrowing增强处理"></a>定义AfterThrowing增强处理</h3><p>使用@AfterThrowing可修饰AfterThrowing增强处理，AfterThrowing增强处理将主要用于处理程序中未处理的异常。<br>使用@AfterThrowing注解可指定如下两个常用属性：</p>
<blockquote>
<ul>
<li>pointcut&#x2F;value：这两个属性的作用是一样的，它们都是用于指定该切入点对应的切入表达式。当指定了pointcut属性之后，value属性值将会被覆盖</li>
<li>thorwing：该属性值指定一个形参名，用于表示Advice方法中可定义与此同名的形参，该形参可用于访问目标方法抛出的异常。除此之外，在Advice方法中定义该形参（代表目标方法抛出的异常）时指定的类型，会限制目标方法必须抛出指定类型的异常</li>
</ul>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 定义一个切面</span></span><br><span class="line"><span class="meta">@Aspect</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">RepairAspect</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="comment">// 匹配org.crazyit.app.service.impl包下所有类的、</span></span><br><span class="line">	<span class="comment">// 所有方法的执行作为切入点</span></span><br><span class="line">	<span class="meta">@AfterThrowing(throwing=&quot;ex&quot;</span></span><br><span class="line"><span class="meta">		, pointcut=&quot;execution(* org.crazyit.app.service.impl.*.*(..))&quot;)</span></span><br><span class="line">	<span class="comment">// 声明ex时指定的类型会限制目标方法必须抛出指定类型的异常</span></span><br><span class="line">	<span class="comment">// 此处将ex的类型声明为Throwable，意味着对目标方法抛出的异常不加限制</span></span><br><span class="line">	<span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">doRecoveryActions</span><span class="params">(Throwable ex)</span></span><br><span class="line">	&#123;</span><br><span class="line">		System.out.println(<span class="string">&quot;目标方法中抛出的异常:&quot;</span> + ex);</span><br><span class="line">		System.out.println(<span class="string">&quot;模拟Advice对异常的修复...&quot;</span>);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>catch捕捉意味着完全处理该异常，如果catch块中没有重新抛出新异常，则该方法可以正常结束；而AfterThrowing处理虽然处理了该异常，但它不能完全处理该异常，该异常依然会传播到上一级调用者</p>
<h3 id="After增强处理"><a href="#After增强处理" class="headerlink" title="After增强处理"></a>After增强处理</h3><p>Spring中提供的After增强处理，它与AfterReturning增强处理类似，但也有区别</p>
<blockquote>
<ul>
<li>AfterReturning增强处理只有在目标方法成功完成后才会被织入</li>
<li>After增强处理不管目标方法如何借宿（包括成功完成和遇到异常终止两种情况），它都会被织入</li>
</ul>
</blockquote>
<p>After增强处理有点类似于finally块<br>使用@After注解时需要指定value属性，该属性用于指定该增强处理被织入的切入点，既可以是一个已有的切入点，也可直接指定切入点表达式</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 定义一个切面</span></span><br><span class="line"><span class="meta">@Aspect</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">ReleaseAspect</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="comment">// 匹配org.crazyit.app.service包下所有类的</span></span><br><span class="line">	<span class="comment">// 所有方法的执行作为切入点</span></span><br><span class="line">	<span class="meta">@After(&quot;execution(* org.crazyit.app.service.*.*(..))&quot;)</span></span><br><span class="line">	<span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">release</span><span class="params">()</span></span><br><span class="line">	&#123;</span><br><span class="line">		System.out.println(<span class="string">&quot;模拟方法结束后的释放资源...&quot;</span>);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="Around增强处理"><a href="#Around增强处理" class="headerlink" title="Around增强处理"></a>Around增强处理</h3><p>@Around注解用于修饰Around增强处理，Around增强处理是功能强大的增强处理，它近似等于Before增强处理和AfterReturning增强处理的总和，Around增强处理既可在执行目标方法之前织入增强动作，也可在执行目标方法之后织入增强动作</p>
<p>Around增强处理可以决定目标方法在什么时候执行，如何执行，甚至可以完全阻止目标方法的执行。Around增强处理可以改变执行目标方法的参数值，也可以改变执行目标方法之后的返回值。</p>
<p>Around通常需要在线程安全的环境下使用。因此如果普通的Before增强处理、AfterReturning增强处理就能解决的问题，则没有必要使用Around增强处理了。如果需要目标方法执行之前和之后共享某种状态数据，则应该考虑使用Around增强处理；尤其是需要改变目标方法的返回值时，则中能使用Around增强处理</p>
<p>Around增强处理使用@Around标注，需要指定一个value属性指定该增强处理被织入的切入点</p>
<p>当定义一个Around增强处理方法时，该方法的第一个形参必须是<strong>ProceedingJoinPoin</strong>类型（至少包含一个形参），在增强方法体内，调用ProceedingJoinPoin参数的proceed()方法才会执行目标方法，如果程序没有调用ProceedingJoinPoin的proceed()方法，则目标方法不会被执行。<br>当调用ProceedingJoinPoin参数的proceed方法时，还可以传入一个Object[]对象最为参数，该数组中的值将被传入目标方法作为执行方法的实参</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 定义一个切面</span></span><br><span class="line"><span class="meta">@Aspect</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">TxAspect</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="comment">// 匹配org.crazyit.app.service.impl包下所有类的、</span></span><br><span class="line">	<span class="comment">// 所有方法的执行作为切入点</span></span><br><span class="line">	<span class="meta">@Around(&quot;execution(* org.crazyit.app.service.impl.*.*(..))&quot;)</span></span><br><span class="line">	<span class="keyword">public</span> Object <span class="title function_">processTx</span><span class="params">(ProceedingJoinPoint jp)</span></span><br><span class="line">		<span class="keyword">throws</span> java.lang.Throwable</span><br><span class="line">	&#123;</span><br><span class="line">		System.out.println(<span class="string">&quot;执行目标方法之前，模拟开始事务...&quot;</span>);</span><br><span class="line">		<span class="comment">// 获取目标方法原始的调用参数</span></span><br><span class="line">		Object[] args = jp.getArgs();</span><br><span class="line">		<span class="keyword">if</span>(args != <span class="literal">null</span> &amp;&amp; args.length &gt; <span class="number">1</span>)</span><br><span class="line">		&#123;</span><br><span class="line">			<span class="comment">// 修改目标方法的第一个参数</span></span><br><span class="line">			args[<span class="number">0</span>] = <span class="string">&quot;【增加的前缀】&quot;</span> + args[<span class="number">0</span>];</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="comment">// 以改变后的参数去执行目标方法，并保存目标方法执行后的返回值</span></span><br><span class="line">		<span class="type">Object</span> <span class="variable">rvt</span> <span class="operator">=</span> jp.proceed(args);</span><br><span class="line">		System.out.println(<span class="string">&quot;执行目标方法之后，模拟结束事务...&quot;</span>);</span><br><span class="line">		<span class="comment">// 如果rvt的类型是Integer，将rvt改为它的平方</span></span><br><span class="line">		<span class="keyword">if</span>(rvt != <span class="literal">null</span> &amp;&amp; rvt <span class="keyword">instanceof</span> Integer)</span><br><span class="line">			rvt = (Integer)rvt * (Integer)rvt;</span><br><span class="line">		<span class="keyword">return</span> rvt;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="访问目标方法的参数"><a href="#访问目标方法的参数" class="headerlink" title="访问目标方法的参数"></a>访问目标方法的参数</h3><p>访问目标方法最简单的做法就是定义增强处理方法时将第一个参数定义为JoinPoint类型，当该增强处理方法被调用时，该JoinPoint参数就代表了织入增强处理的连接点。JoinPoint里包含如下几个常用方法：</p>
<blockquote>
<p><code>Object[] getArg()</code>：返回执行目标方法时的参数<br><code>Signature getSignature()</code>：返回被增强的方法的相关信息<br><code>Object getTarget()</code>：返回被织入增强处理的目标参数<br><code>Object getThis()</code>：返回AOP框架为目标对象生成的代理对象</p>
</blockquote>
<p>ProceedingJoinPoin就是JoinPoint方法的子类</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 定义一个切面</span></span><br><span class="line"><span class="meta">@Aspect</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">FourAdviceTest</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="comment">// 定义Around增强处理</span></span><br><span class="line">	<span class="meta">@Around(&quot;execution(* org.crazyit.app.service.impl.*.*(..))&quot;)</span></span><br><span class="line">	<span class="keyword">public</span> Object <span class="title function_">processTx</span><span class="params">(ProceedingJoinPoint jp)</span></span><br><span class="line">		<span class="keyword">throws</span> java.lang.Throwable</span><br><span class="line">	&#123;</span><br><span class="line">		System.out.println(<span class="string">&quot;Around增强：执行目标方法之前，模拟开始事务...&quot;</span>);</span><br><span class="line">		<span class="comment">// 访问执行目标方法的参数</span></span><br><span class="line">		Object[] args = jp.getArgs();</span><br><span class="line">		<span class="comment">// 当执行目标方法的参数存在，</span></span><br><span class="line">		<span class="comment">// 且第一个参数是字符串参数</span></span><br><span class="line">		<span class="keyword">if</span> (args != <span class="literal">null</span> &amp;&amp; args.length &gt; <span class="number">0</span></span><br><span class="line">			&amp;&amp; args[<span class="number">0</span>].getClass() == String.class)</span><br><span class="line">		&#123;</span><br><span class="line">			<span class="comment">// 修改目标方法调用参数的第一个参数</span></span><br><span class="line">			args[<span class="number">0</span>] = <span class="string">&quot;【增加的前缀】&quot;</span> + args[<span class="number">0</span>];</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="comment">//执行目标方法，并保存目标方法执行后的返回值</span></span><br><span class="line">		<span class="type">Object</span> <span class="variable">rvt</span> <span class="operator">=</span> jp.proceed(args);</span><br><span class="line">		System.out.println(<span class="string">&quot;Around增强：执行目标方法之后，模拟结束事务...&quot;</span>);</span><br><span class="line">		<span class="comment">// 如果rvt的类型是Integer，将rvt改为它的平方</span></span><br><span class="line">		<span class="keyword">if</span>(rvt != <span class="literal">null</span> &amp;&amp; rvt <span class="keyword">instanceof</span> Integer)</span><br><span class="line">			rvt = (Integer)rvt * (Integer)rvt;</span><br><span class="line">		<span class="keyword">return</span> rvt;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">// 定义Before增强处理</span></span><br><span class="line">	<span class="meta">@Before(&quot;execution(* org.crazyit.app.service.impl.*.*(..))&quot;)</span></span><br><span class="line">	<span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">authority</span><span class="params">(JoinPoint jp)</span></span><br><span class="line">	&#123;</span><br><span class="line">		System.out.println(<span class="string">&quot;Before增强：模拟执行权限检查&quot;</span>);</span><br><span class="line">		<span class="comment">// 返回被织入增强处理的目标方法</span></span><br><span class="line">		System.out.println(<span class="string">&quot;Before增强：被织入增强处理的目标方法为：&quot;</span></span><br><span class="line">			+ jp.getSignature().getName());</span><br><span class="line">		<span class="comment">// 访问执行目标方法的参数</span></span><br><span class="line">		System.out.println(<span class="string">&quot;Before增强：目标方法的参数为：&quot;</span></span><br><span class="line">			+ Arrays.toString(jp.getArgs()));</span><br><span class="line">		<span class="comment">// 访问被增强处理的目标对象</span></span><br><span class="line">		System.out.println(<span class="string">&quot;Before增强：被织入增强处理的目标对象为：&quot;</span></span><br><span class="line">			+ jp.getTarget());</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">//定义AfterReturning增强处理</span></span><br><span class="line">	<span class="meta">@AfterReturning(pointcut=&quot;execution(* org.crazyit.app.service.impl.*.*(..))&quot;</span></span><br><span class="line"><span class="meta">		, returning=&quot;rvt&quot;)</span></span><br><span class="line">	<span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">log</span><span class="params">(JoinPoint jp , Object rvt)</span></span><br><span class="line">	&#123;</span><br><span class="line">		System.out.println(<span class="string">&quot;AfterReturning增强：获取目标方法返回值:&quot;</span></span><br><span class="line">			+ rvt);</span><br><span class="line">		System.out.println(<span class="string">&quot;AfterReturning增强：模拟记录日志功能...&quot;</span>);</span><br><span class="line">		<span class="comment">// 返回被织入增强处理的目标方法</span></span><br><span class="line">		System.out.println(<span class="string">&quot;AfterReturning增强：被织入增强处理的目标方法为：&quot;</span></span><br><span class="line">			+ jp.getSignature().getName());</span><br><span class="line">		<span class="comment">// 访问执行目标方法的参数</span></span><br><span class="line">		System.out.println(<span class="string">&quot;AfterReturning增强：目标方法的参数为：&quot;</span></span><br><span class="line">			+ Arrays.toString(jp.getArgs()));</span><br><span class="line">		<span class="comment">// 访问被增强处理的目标对象</span></span><br><span class="line">		System.out.println(<span class="string">&quot;AfterReturning增强：被织入增强处理的目标对象为：&quot;</span></span><br><span class="line">			+ jp.getTarget());</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="comment">// 定义After增强处理</span></span><br><span class="line">	<span class="meta">@After(&quot;execution(* org.crazyit.app.service.impl.*.*(..))&quot;)</span></span><br><span class="line">	<span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">release</span><span class="params">(JoinPoint jp)</span></span><br><span class="line">	&#123;</span><br><span class="line">		System.out.println(<span class="string">&quot;After增强：模拟方法结束后的释放资源...&quot;</span>);</span><br><span class="line">		<span class="comment">// 返回被织入增强处理的目标方法</span></span><br><span class="line">		System.out.println(<span class="string">&quot;After增强：被织入增强处理的目标方法为：&quot;</span></span><br><span class="line">			+ jp.getSignature().getName());</span><br><span class="line">		<span class="comment">// 访问执行目标方法的参数</span></span><br><span class="line">		System.out.println(<span class="string">&quot;After增强：目标方法的参数为：&quot;</span></span><br><span class="line">			+ Arrays.toString(jp.getArgs()));</span><br><span class="line">		<span class="comment">// 访问被增强处理的目标对象</span></span><br><span class="line">		System.out.println(<span class="string">&quot;After增强：被织入增强处理的目标对象为：&quot;</span></span><br><span class="line">			+ jp.getTarget());</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>在“进入”连接点时，具有最高优先级的增强处理将被优先织入（所以在给定的两个Befoe增强处理中，优先级高的哪个会先执行）。在“退出”连接点时，具有最高优先级的增强处理会最后被织入（所以在给定的两个After增强处理中，优先级高的那个会后执行）</p>
<p>当不同切面里的两个增强处理需要在用一个连接点被织入时，Spring AOP将以随机的顺序来织入这两个增强处理。如果应用需要制定不同切面类里增强处理的优先级，Spring提供了如下两种解决方案：</p>
<blockquote>
<ul>
<li>让切面类实现org.springframework.core.Ordered接口，实现该接口只需实现一个int getOrder()方法，该方法的返回值越小，则优先级越高</li>
<li>直接使用@Order注解来修饰一个切面类，使用@Order注解时可指定一个int型value属性，该属性值越小，则优先级越高</li>
</ul>
</blockquote>
<p>如果只需要访问目标方法的参数，Spring还提供了一种更简单的方法：可以在程序中使用args切入点表达式来绑定目标方法的参数。如果在一个args表达式中指定了一个或多个参数，则该切入点将只匹配具有相应形参的方法，且目标方法的参数值将百日传入增强处理方法</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Aspect</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">AccessArgAspect</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="comment">// 下面的args(arg0,arg1)会限制目标方法必须有2个形参</span></span><br><span class="line">	<span class="meta">@AfterReturning(returning=&quot;rvt&quot; , pointcut=</span></span><br><span class="line"><span class="meta">		&quot;execution(* org.crazyit.app.service.impl.*.*(..)) &amp;&amp; args(arg0,arg1)&quot;)</span></span><br><span class="line">	<span class="comment">// 此处指定arg0、arg1为String类型</span></span><br><span class="line">	<span class="comment">// 则args(arg0,arg1)还要求目标方法的两个形参都是String类型</span></span><br><span class="line">	<span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">access</span><span class="params">(Object rvt, String arg0 , String arg1)</span></span><br><span class="line">	&#123;</span><br><span class="line">		System.out.println(<span class="string">&quot;调用目标方法第1个参数为:&quot;</span> + arg0);</span><br><span class="line">		System.out.println(<span class="string">&quot;调用目标方法第2个参数为:&quot;</span> + arg1);</span><br><span class="line">		System.out.println(<span class="string">&quot;获取目标方法返回值:&quot;</span> + rvt);</span><br><span class="line">		System.out.println(<span class="string">&quot;模拟记录日志功能...&quot;</span>);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>使用args表达式有如下两个作用：</p>
<blockquote>
<ul>
<li>提供了一种简单的方式来访问目标方法的参数</li>
<li>对表达式增加额外的限制</li>
</ul>
</blockquote>
<h3 id="定义切入点"><a href="#定义切入点" class="headerlink" title="定义切入点"></a>定义切入点</h3><p>SpringAOP只支持将Spring Bean的方法执行作为连接点，所以可以把切入点看成所有能和切入点表达式匹配的Bean的方法<br>切入点定义包含两部分：</p>
<blockquote>
<ul>
<li>一个切入点表达式：用于指定改切入点和哪些方法进行匹配</li>
<li>一个包含名字和任意参数的方法签名：作为该切入点的名称</li>
</ul>
</blockquote>
<p>在@AspectJ风格的AOP中，切入点签名必须采用一个普通的方法定义（方法体通常为空）来提供，且该方法的返回值必须是void；切入点表达式需要使用@Pointcut注解来标注</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 使用@Ponitcut注解定义切入点</span></span><br><span class="line"><span class="meta">@Pointcut(&quot;execution(* transfer(..))&quot;)</span></span><br><span class="line"><span class="comment">// 使用一个返回值为void、方法体为空的方法来命名切入点</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">anyOldTransfer</span><span class="params">()</span>&#123;&#125;</span><br></pre></td></tr></table></figure>

<p>如果需要使用本切面类的切入点，则可使用@Before、@After、@Around等注解定义Advice时，使用pointcut或value属性值引用已有的切入点</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@AfterReturning(pointcut=&quot;mypoint()&quot;,returning=&quot;relval&quot;)</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">writelog</span><span class="params">(String msg, Object retval)</span></span><br><span class="line">&#123;</span><br><span class="line">    ...</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>如果需要使用其他切面类中的切入点，则其他切面类中的切入点不能使用private修饰。而且在使用@Before、@After、@Around等注解中的pointcut或value属性值引用已有的切入点时，必须添加类名前缀</p>
<h3 id="切入点指示符"><a href="#切入点指示符" class="headerlink" title="切入点指示符"></a>切入点指示符</h3><p>SpringAOP一共支持如下几种方法的连接点：</p>
<blockquote>
<ul>
<li><p>execution：用于匹配执行方法的连接点，execution表达式的格式如下：</p>
<p><code>execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern) throw-pattern?)</code></p>
<blockquote>
<ul>
<li>modifiers-pattern：指定方法的修饰符，支持通配符，可省略</li>
<li>ret-type-pattern：指定方法的返回值类型，支持通配符，可以使用“*”通配符来匹配所用的返回值类型</li>
<li>declaring-type-pattern：指定方法所属的类，支持通配符，可省略</li>
<li>name-pattern：指定匹配指定的方法名，支持通配符，可以使用“*”通配符来匹配所有方法</li>
<li>param-pattern：指定方法声明中的形参列表，支持两个通配符，“*”代表一个任意类型的参数，“..”代表零个或多个任意类型的参数</li>
<li>throw-pattern：指定方法声明抛出的异常，支持通配符，可省略</li>
</ul>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 匹配任意public方法的执行</span></span><br><span class="line">execution(<span class="keyword">public</span> * * (..))</span><br><span class="line"><span class="comment">// 匹配任何方法名以“set”开始的方法的执行</span></span><br><span class="line">execution(* set*(..))</span><br><span class="line"><span class="comment">// 匹配AccountServiceImpl中任意方法的执行</span></span><br><span class="line">execution(* org.learn.impl.AccountServiceImpl.* (..))</span><br><span class="line"><span class="comment">// 匹配org.learn.impl.包中任意类的任意方法</span></span><br><span class="line">execution(* org.learn.impl.*.* (..))</span><br></pre></td></tr></table></figure></li>
<li><p>within：用于限定匹配特定类型的连接点，在SpringAOP中只是方法执行的连接点</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 在org.learn.app.service包中的任意连接点（在SpringAOP中只是方法执行的连接点）</span></span><br><span class="line">whitin(org.learn.app.service.*) </span><br><span class="line"><span class="comment">// 在org.learn.app.service包或其子包中的任意连接点（在SpringAOP中只是方法执行的连接点）</span></span><br><span class="line">whitin(org.learn.app.service..*) </span><br></pre></td></tr></table></figure></li>
<li><p>this：用于限定AOP代理必须是指定类型的实例，匹配该对象的所有连接点。在SpringAOP中只是方法执行的连接点</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 匹配实现了 org.learn.app.service.AccountService接口的 AOP 代理的所有连接点</span></span><br><span class="line"><span class="built_in">this</span>(org.learn.app.service.AccountService)</span><br></pre></td></tr></table></figure>
</li>
<li><p>target 用于限定目标对象必须是指定类型的实例，匹配该对象的所有连接点。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 匹配实现了 org.learn.app.service.AccountService接口的目标对象的所有连接点</span></span><br><span class="line"><span class="built_in">this</span>(org.learn.app.service.AccountService)</span><br></pre></td></tr></table></figure>
</li>
<li><p>args：用于对连接点的参数类型进行限制，要求参数类型必须是知道那个类型</p>
</li>
<li><p>bean：用于限定值匹配指定 Bean 实例内的连接点</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 匹配 tradeService Bean 实例内方法执行的连接点</span></span><br><span class="line">bean(tradeService)</span><br><span class="line"><span class="comment">// 匹配名字以 Serivice 结尾的 Bean 实例内方法执行的连接点</span></span><br><span class="line">bean(*Service)</span><br></pre></td></tr></table></figure></li>
</ul>
</blockquote>
<h3 id="组合切入点表达式"><a href="#组合切入点表达式" class="headerlink" title="组合切入点表达式"></a>组合切入点表达式</h3><p>Spring 支持如下三个逻辑运算符来组合切入点表达式</p>
<blockquote>
<ul>
<li><code>&amp;&amp;</code>：要求连接点同时匹配两个切入点表达式</li>
<li><code>||</code>：要求连接点匹配任意个切入点表达式</li>
<li><code>!:</code>：要求连接点不匹配指定的切入点表达式</li>
</ul>
</blockquote>
<h2 id="基于-XML-配置文件的管理方式"><a href="#基于-XML-配置文件的管理方式" class="headerlink" title="基于 XML 配置文件的管理方式"></a>基于 XML 配置文件的管理方式</h2><p>在 Spring 配置文件中，所有的切面、切入点和增强处理都必须定义在<code>&lt;aop:config.../&gt;</code>元素内部。<code>&lt;beans.../&gt;</code>元素下可以包含多个<code>&lt;aop:config.../&gt;</code>元素，一个<code>&lt;aop:config&gt;</code>可以包含 pointcut、advisor 和 aspect 元素，且这三个元素必须按照此顺序来定义。</p>
<h3 id="配置切面"><a href="#配置切面" class="headerlink" title="配置切面"></a>配置切面</h3><p>因为切面 Bean 可以当成一个普通的 Spring Bean 来配置，所以完全可以为该切面 Bean 配置依赖注入。当切面 Bean 定义完成后，通过在<code>&lt;aop:aspect&gt;</code>元素中使用 ref 属性来引用该 Bean，就可将该 Bean 转换成一个切面 Bean了。</p>
<p>配置<code>&lt;aop:aspect&gt;</code>元素时可以指定如下三个属性：</p>
<blockquote>
<ul>
<li>id：定义该切面的标识名</li>
<li>ref：用于将 ref 属性所引用的普通 Bean 转换为切面 Bean</li>
<li>order：指定切面 Bean 的优先级，值越小，优先级越高</li>
</ul>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">&lt;aop:config&gt;</span><br><span class="line">    &lt;!-- 将容器中的 afterAdiveBean 转换成切面 Bean </span><br><span class="line">        切面 Bean 的新名称为：afterAdviceAspect --&gt;</span><br><span class="line">    &lt;aop:aspect id=&quot;afterAdviceAspect&quot; ref=&quot;afterAdviceBean&quot; &gt;</span><br><span class="line">    &lt;/aop:aspect&gt;</span><br><span class="line">&lt;/aop:config&gt;</span><br><span class="line">&lt;!-- 定义一个普通 Bean 实例，该 Bean 实例将作为 Aspect Bean --&gt;</span><br><span class="line">&lt;bean id=&quot;afterAdviceBean&quot; class=&quot;lee.AfterAdviceTest&quot;/&gt;</span><br></pre></td></tr></table></figure>

<h3 id="配置增强处理"><a href="#配置增强处理" class="headerlink" title="配置增强处理"></a>配置增强处理</h3><p>使用 XML 配置增强处理依赖如下几个元素：</p>
<blockquote>
<ul>
<li><code>&lt;aop:before.../&gt;</code>：配置 Before 增强处理</li>
<li><code>&lt;aop:after.../&gt;</code>：配置 After 增强处理</li>
<li><code>&lt;aop:after-returning.../&gt;</code>：配置 AfterReturning 增强处理</li>
<li><code>&lt;aop:after-throwing.../&gt;</code>：配置 AfterThrowing 增强处理</li>
<li><code>&lt;aop:around.../&gt;</code>：配置 Around 增强处理</li>
</ul>
</blockquote>
<p>上面的这些元素都不支持使用子元素，但通常可指定如下属性：</p>
<blockquote>
<ul>
<li>pointcut|pointcut-ref：pointcut 属性将指定一个切入表达式，pointcut-ref 属性指定以后的切入点名称，Spring 将在匹配表达式的连接点时织入该增强处理。通常这两个属性只使用一个</li>
<li>method：该属性指定一个方法名，指定将切面Bean的该方法转换为增强处理</li>
<li>throwing：该属性只对<code>&lt;aop:after-throwing.../&gt;</code>元素有效，用于指定一个形参名，AfterThrowing增强处理方法可以通过该形参访问目标方法所抛出的异常</li>
<li>returning：该属性只对<code>&lt;aop:after-returning.../&gt;</code>元素有效，用于指定一个形参名，AfterReturning增强处理方法可通过该属性访问目标方法的返回值</li>
</ul>
</blockquote>
<p>XML配置方式和@AspectJ注解方式一样支持组合切入点表达式，但XML配置方式不再使用简单的&amp;&amp;、||和!作为组合表达式，而是使用如下三个组合运算符：and、or、not</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">beans</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">aop:config</span>&gt;</span></span><br><span class="line">		<span class="comment">&lt;!-- 将fourAdviceBean转换成切面Bean</span></span><br><span class="line"><span class="comment">			切面Bean的新名称为：fourAdviceAspect</span></span><br><span class="line"><span class="comment">			指定该切面的优先级为2 --&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">aop:aspect</span> <span class="attr">id</span>=<span class="string">&quot;fourAdviceAspect&quot;</span> <span class="attr">ref</span>=<span class="string">&quot;fourAdviceBean&quot;</span></span></span><br><span class="line"><span class="tag">			<span class="attr">order</span>=<span class="string">&quot;2&quot;</span>&gt;</span></span><br><span class="line">			<span class="comment">&lt;!-- 定义一个After增强处理</span></span><br><span class="line"><span class="comment">				直接指定切入点表达式</span></span><br><span class="line"><span class="comment">				以切面Bean中的release()方法作为增强处理方法 --&gt;</span></span><br><span class="line">			<span class="tag">&lt;<span class="name">aop:after</span> <span class="attr">pointcut</span>=<span class="string">&quot;execution(* org.crazyit.app.service.impl.*.*(..))&quot;</span> </span></span><br><span class="line"><span class="tag">				<span class="attr">method</span>=<span class="string">&quot;release&quot;</span>/&gt;</span></span><br><span class="line">			<span class="comment">&lt;!-- 定义一个Before增强处理</span></span><br><span class="line"><span class="comment">				直接指定切入点表达式</span></span><br><span class="line"><span class="comment">				以切面Bean中的authority()方法作为增强处理方法 --&gt;</span></span><br><span class="line">			<span class="tag">&lt;<span class="name">aop:before</span> <span class="attr">pointcut</span>=<span class="string">&quot;execution(* org.crazyit.app.service.impl.*.*(..))&quot;</span> </span></span><br><span class="line"><span class="tag">				<span class="attr">method</span>=<span class="string">&quot;authority&quot;</span>/&gt;</span></span><br><span class="line">			<span class="comment">&lt;!-- 定义一个AfterReturning增强处理</span></span><br><span class="line"><span class="comment">				直接指定切入点表达式</span></span><br><span class="line"><span class="comment">				以切面Bean中的log()方法作为增强处理方法 --&gt;</span></span><br><span class="line">			<span class="tag">&lt;<span class="name">aop:after-returning</span> <span class="attr">pointcut</span>=<span class="string">&quot;execution(* org.crazyit.app.service.impl.*.*(..))&quot;</span></span></span><br><span class="line"><span class="tag">				<span class="attr">method</span>=<span class="string">&quot;log&quot;</span> <span class="attr">returning</span>=<span class="string">&quot;rvt&quot;</span>/&gt;</span></span><br><span class="line">			<span class="comment">&lt;!-- 定义一个Around增强处理</span></span><br><span class="line"><span class="comment">				直接指定切入点表达式</span></span><br><span class="line"><span class="comment">				以切面Bean中的processTx()方法作为增强处理方法 --&gt;</span></span><br><span class="line">			<span class="tag">&lt;<span class="name">aop:around</span> <span class="attr">pointcut</span>=<span class="string">&quot;execution(* org.crazyit.app.service.impl.*.*(..))&quot;</span> </span></span><br><span class="line"><span class="tag">				<span class="attr">method</span>=<span class="string">&quot;processTx&quot;</span>/&gt;</span></span><br><span class="line">		<span class="tag">&lt;/<span class="name">aop:aspect</span>&gt;</span></span><br><span class="line">		<span class="comment">&lt;!-- 将secondAdviceBean转换成切面Bean</span></span><br><span class="line"><span class="comment">			切面Bean的新名称为：secondAdviceAspect</span></span><br><span class="line"><span class="comment">			指定该切面的优先级为1，该切面里的增强处理将被优先织入 --&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">aop:aspect</span> <span class="attr">id</span>=<span class="string">&quot;secondAdviceAspect&quot;</span> <span class="attr">ref</span>=<span class="string">&quot;secondAdviceBean&quot;</span></span></span><br><span class="line"><span class="tag">			<span class="attr">order</span>=<span class="string">&quot;1&quot;</span>&gt;</span></span><br><span class="line">			<span class="comment">&lt;!-- 定义一个Before增强处理</span></span><br><span class="line"><span class="comment">				直接指定切入点表达式</span></span><br><span class="line"><span class="comment">				以切面Bean中的authority()方法作为增强处理方法 </span></span><br><span class="line"><span class="comment">				且该参数必须为String类型（由authority方法声明中aa参数的类型决定） --&gt;</span></span><br><span class="line">			<span class="tag">&lt;<span class="name">aop:before</span> <span class="attr">pointcut</span>=</span></span><br><span class="line"><span class="tag">			<span class="string">&quot;execution(* org.crazyit.app.service.impl.*.*(..)) and args(aa,..)&quot;</span> </span></span><br><span class="line"><span class="tag">				<span class="attr">method</span>=<span class="string">&quot;authority&quot;</span>/&gt;</span></span><br><span class="line">		<span class="tag">&lt;/<span class="name">aop:aspect</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;/<span class="name">aop:config</span>&gt;</span></span><br><span class="line">	<span class="comment">&lt;!-- 定义一个普通Bean实例，该Bean实例将被作为Aspect Bean --&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">bean</span> <span class="attr">id</span>=<span class="string">&quot;fourAdviceBean&quot;</span></span></span><br><span class="line"><span class="tag">		<span class="attr">class</span>=<span class="string">&quot;org.crazyit.app.aspect.FourAdviceTest&quot;</span>/&gt;</span></span><br><span class="line">	<span class="comment">&lt;!-- 再定义一个普通Bean实例，该Bean实例将被作为Aspect Bean --&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">bean</span> <span class="attr">id</span>=<span class="string">&quot;secondAdviceBean&quot;</span></span></span><br><span class="line"><span class="tag">		<span class="attr">class</span>=<span class="string">&quot;org.crazyit.app.aspect.SecondAdviceTest&quot;</span>/&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">bean</span> <span class="attr">id</span>=<span class="string">&quot;hello&quot;</span> <span class="attr">class</span>=<span class="string">&quot;org.crazyit.app.service.impl.HelloImpl&quot;</span>/&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">bean</span> <span class="attr">id</span>=<span class="string">&quot;world&quot;</span> <span class="attr">class</span>=<span class="string">&quot;org.crazyit.app.service.impl.WorldImpl&quot;</span>/&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">beans</span>&gt;</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="配置切入点"><a href="#配置切入点" class="headerlink" title="配置切入点"></a>配置切入点</h3><p>Spring提供了<code>&lt;aop:pointcut.../&gt;</code>元素来定义切入点。当把<code>&lt;aop:pointcut&gt;</code>元素作为<code>&lt;aop:config.../&gt;</code>的子元素定义时，表明该切入点可被多个切面共享；当把<code>&lt;aop:pointcut.../&gt;</code>元素作为<code>&lt;aop:aspect.../&gt;</code>子元素定义时，表明该切入点只能在该切面中有效</p>
<p>配置<code>&lt;aop:pointcut.../&gt;</code>元素时通常需要指定如下两个属性：</p>
<blockquote>
<ul>
<li>id：指定该切入点的标识名</li>
<li>expression：指定该切入点关联的切入点表达式</li>
</ul>
</blockquote>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">beans</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">aop:config</span>&gt;</span></span><br><span class="line">		<span class="comment">&lt;!-- 定义一个切入点：myPointcut</span></span><br><span class="line"><span class="comment">			通过expression指定它对应的切入点表达式 --&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">aop:pointcut</span> <span class="attr">id</span>=<span class="string">&quot;myPointcut&quot;</span> </span></span><br><span class="line"><span class="tag">			<span class="attr">expression</span>=<span class="string">&quot;execution(* org.crazyit.app.service.impl.*.*(..))&quot;</span>/&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">aop:aspect</span> <span class="attr">id</span>=<span class="string">&quot;afterThrowingAdviceAspect&quot;</span></span></span><br><span class="line"><span class="tag">			<span class="attr">ref</span>=<span class="string">&quot;afterThrowingAdviceBean&quot;</span>&gt;</span></span><br><span class="line">			<span class="comment">&lt;!-- 定义一个AfterThrowing增强处理，指定切入点</span></span><br><span class="line"><span class="comment">				以切面Bean中的doRecoveryActions()方法作为增强处理方法 --&gt;</span></span><br><span class="line">			<span class="tag">&lt;<span class="name">aop:after-throwing</span> <span class="attr">pointcut-ref</span>=<span class="string">&quot;myPointcut&quot;</span> </span></span><br><span class="line"><span class="tag">				<span class="attr">method</span>=<span class="string">&quot;doRecoveryActions&quot;</span> <span class="attr">throwing</span>=<span class="string">&quot;ex&quot;</span>/&gt;</span></span><br><span class="line">		<span class="tag">&lt;/<span class="name">aop:aspect</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;/<span class="name">aop:config</span>&gt;</span></span><br><span class="line">	<span class="comment">&lt;!-- 定义一个普通Bean实例，该Bean实例将被作为Aspect Bean --&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">bean</span> <span class="attr">id</span>=<span class="string">&quot;afterThrowingAdviceBean&quot;</span></span></span><br><span class="line"><span class="tag">		<span class="attr">class</span>=<span class="string">&quot;org.crazyit.app.aspect.RepairAspect&quot;</span>/&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">bean</span> <span class="attr">id</span>=<span class="string">&quot;hello&quot;</span> <span class="attr">class</span>=<span class="string">&quot;org.crazyit.app.service.impl.HelloImpl&quot;</span>/&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">bean</span> <span class="attr">id</span>=<span class="string">&quot;world&quot;</span> <span class="attr">class</span>=<span class="string">&quot;org.crazyit.app.service.impl.WorldImpl&quot;</span>/&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">beans</span>&gt;</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="http://yzb317415.gitee.io/blog">Milk Yang</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="http://yzb317415.gitee.io/blog/2022/06/22/SpringAOP/">http://yzb317415.gitee.io/blog/2022/06/22/SpringAOP/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="http://yzb317415.gitee.io/blog" target="_blank">梓琰苑</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/blog/tags/Spring/">Spring</a></div><div class="post_share"><div class="social-share" data-image="" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/gh/overtrue/share.js@master/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="next-post pull-full"><a href="/blog/2022/06/22/%E9%9C%80%E6%B1%82%E5%BC%80%E5%8F%91/"><img class="next-cover" src="" onerror="onerror=null;src='/blog/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">需求开发</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/blog/2022/06/22/Spring%E5%85%A5%E9%97%A8/" title="Spring入门"><img class="cover" src="" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-06-22</div><div class="title">Spring入门</div></div></a></div><div><a href="/blog/2022/06/22/Spring%E7%9A%84%E4%BA%8B%E5%8A%A1/" title="Spring 的事务"><img class="cover" src="" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-06-22</div><div class="title">Spring 的事务</div></div></a></div><div><a href="/blog/2022/06/22/Spring%E7%9A%84%E7%BC%93%E5%AD%98%E6%9C%BA%E5%88%B6/" title="Spring的缓存机制"><img class="cover" src="" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-06-22</div><div class="title">Spring的缓存机制</div></div></a></div><div><a href="/blog/2022/06/22/%E6%B7%B1%E5%85%A5%E4%BD%BF%E7%94%A8%20Spring/" title="深入使用 Spring"><img class="cover" src="" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-06-22</div><div class="title">深入使用 Spring</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="https://i.loli.net/2021/02/24/5O1day2nriDzjSu.png" onerror="this.onerror=null;this.src='/blog/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">Milk Yang</div><div class="author-info__description">不积跬步无以至千里，不积小流无以成江海</div></div><div class="card-info-data site-data is-center"><a href="/blog/archives/"><div class="headline">文章</div><div class="length-num">8</div></a><a href="/blog/tags/"><div class="headline">标签</div><div class="length-num">2</div></a><a href="/blog/categories/"><div class="headline">分类</div><div class="length-num">1</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/xxxxxx"><i class="fab fa-github"></i><span>Follow Me</span></a></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">This is my Blog</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#Spring%E7%9A%84AOP"><span class="toc-number">1.</span> <span class="toc-text">Spring的AOP</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#AOP%E7%9A%84%E5%9F%BA%E6%9C%AC%E6%A6%82%E5%BF%B5"><span class="toc-number">1.1.</span> <span class="toc-text">AOP的基本概念</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%9F%BA%E4%BA%8E%E6%B3%A8%E8%A7%A3%E7%9A%84%E2%80%9C%E9%9B%B6%E9%85%8D%E7%BD%AE%E2%80%9D%E6%96%B9%E5%BC%8F"><span class="toc-number">1.2.</span> <span class="toc-text">基于注解的“零配置”方式</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%9A%E4%B9%89%E5%88%87%E9%9D%A2Bean"><span class="toc-number">1.2.1.</span> <span class="toc-text">定义切面Bean</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%9A%E4%B9%89Before%E5%A2%9E%E5%BC%BA%E5%A4%84%E7%90%86"><span class="toc-number">1.2.2.</span> <span class="toc-text">定义Before增强处理</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%9A%E4%B9%89AfterReturning%E5%A2%9E%E5%BC%BA%E5%A4%84%E7%90%86"><span class="toc-number">1.2.3.</span> <span class="toc-text">定义AfterReturning增强处理</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%9A%E4%B9%89AfterThrowing%E5%A2%9E%E5%BC%BA%E5%A4%84%E7%90%86"><span class="toc-number">1.2.4.</span> <span class="toc-text">定义AfterThrowing增强处理</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#After%E5%A2%9E%E5%BC%BA%E5%A4%84%E7%90%86"><span class="toc-number">1.2.5.</span> <span class="toc-text">After增强处理</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Around%E5%A2%9E%E5%BC%BA%E5%A4%84%E7%90%86"><span class="toc-number">1.2.6.</span> <span class="toc-text">Around增强处理</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%AE%BF%E9%97%AE%E7%9B%AE%E6%A0%87%E6%96%B9%E6%B3%95%E7%9A%84%E5%8F%82%E6%95%B0"><span class="toc-number">1.2.7.</span> <span class="toc-text">访问目标方法的参数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%9A%E4%B9%89%E5%88%87%E5%85%A5%E7%82%B9"><span class="toc-number">1.2.8.</span> <span class="toc-text">定义切入点</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%88%87%E5%85%A5%E7%82%B9%E6%8C%87%E7%A4%BA%E7%AC%A6"><span class="toc-number">1.2.9.</span> <span class="toc-text">切入点指示符</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%BB%84%E5%90%88%E5%88%87%E5%85%A5%E7%82%B9%E8%A1%A8%E8%BE%BE%E5%BC%8F"><span class="toc-number">1.2.10.</span> <span class="toc-text">组合切入点表达式</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%9F%BA%E4%BA%8E-XML-%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6%E7%9A%84%E7%AE%A1%E7%90%86%E6%96%B9%E5%BC%8F"><span class="toc-number">1.3.</span> <span class="toc-text">基于 XML 配置文件的管理方式</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%85%8D%E7%BD%AE%E5%88%87%E9%9D%A2"><span class="toc-number">1.3.1.</span> <span class="toc-text">配置切面</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%85%8D%E7%BD%AE%E5%A2%9E%E5%BC%BA%E5%A4%84%E7%90%86"><span class="toc-number">1.3.2.</span> <span class="toc-text">配置增强处理</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%85%8D%E7%BD%AE%E5%88%87%E5%85%A5%E7%82%B9"><span class="toc-number">1.3.3.</span> <span class="toc-text">配置切入点</span></a></li></ol></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/blog/2022/06/22/SpringAOP/" title="SpringAOP"><img src="" onerror="this.onerror=null;this.src='/blog/img/404.jpg'" alt="SpringAOP"/></a><div class="content"><a class="title" href="/blog/2022/06/22/SpringAOP/" title="SpringAOP">SpringAOP</a><time datetime="2022-06-22T10:06:45.841Z" title="发表于 2022-06-22 18:06:45">2022-06-22</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blog/2022/06/22/%E9%9C%80%E6%B1%82%E5%BC%80%E5%8F%91/" title="需求开发"><img src="" onerror="this.onerror=null;this.src='/blog/img/404.jpg'" alt="需求开发"/></a><div class="content"><a class="title" href="/blog/2022/06/22/%E9%9C%80%E6%B1%82%E5%BC%80%E5%8F%91/" title="需求开发">需求开发</a><time datetime="2022-06-22T10:06:45.837Z" title="发表于 2022-06-22 18:06:45">2022-06-22</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blog/2022/06/22/%E6%B7%B1%E5%85%A5%E4%BD%BF%E7%94%A8%20Spring/" title="深入使用 Spring"><img src="" onerror="this.onerror=null;this.src='/blog/img/404.jpg'" alt="深入使用 Spring"/></a><div class="content"><a class="title" href="/blog/2022/06/22/%E6%B7%B1%E5%85%A5%E4%BD%BF%E7%94%A8%20Spring/" title="深入使用 Spring">深入使用 Spring</a><time datetime="2022-06-22T10:06:45.833Z" title="发表于 2022-06-22 18:06:45">2022-06-22</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blog/2022/06/22/%E8%BD%AF%E4%BB%B6%E9%9C%80%E6%B1%82%E7%9A%84%E6%9C%AC%E8%B4%A8/" title="软件需求的本质"><img src="" onerror="this.onerror=null;this.src='/blog/img/404.jpg'" alt="软件需求的本质"/></a><div class="content"><a class="title" href="/blog/2022/06/22/%E8%BD%AF%E4%BB%B6%E9%9C%80%E6%B1%82%E7%9A%84%E6%9C%AC%E8%B4%A8/" title="软件需求的本质">软件需求的本质</a><time datetime="2022-06-22T10:06:45.828Z" title="发表于 2022-06-22 18:06:45">2022-06-22</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blog/2022/06/22/Spring%E5%85%A5%E9%97%A8/" title="Spring入门"><img src="" onerror="this.onerror=null;this.src='/blog/img/404.jpg'" alt="Spring入门"/></a><div class="content"><a class="title" href="/blog/2022/06/22/Spring%E5%85%A5%E9%97%A8/" title="Spring入门">Spring入门</a><time datetime="2022-06-22T10:06:45.824Z" title="发表于 2022-06-22 18:06:45">2022-06-22</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2022 By Milk Yang</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/blog/js/utils.js"></script><script src="/blog/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.js"></script><div class="js-pjax"></div><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>